package reflect;

import reflect.anno.AutoRunClass;
import reflect.anno.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test6 {
        public static void main(String[] args) throws Exception{
            long l = System.currentTimeMillis();
            File dir = new File(
                    Test3.class.getResource(".").toURI()
            );
            String packname = Test5.class.getPackage().getName();
            File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
            for (File file : files) {
                String substring = file.getName().substring(0, file.getName().indexOf("."));
                Class<?> aClass = Class.forName(packname + "." + substring);
                if (aClass.isAnnotationPresent(AutoRunClass.class)) {
                    Method[] methods = aClass.getDeclaredMethods();
                    Object o = aClass.newInstance();
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(AutoRunMethod.class)){
                            int value = method.getAnnotation(AutoRunMethod.class).value();
                            System.out.println(method.getName()+"方法"+value+"次");
                            for (int i = 0; i < value; i++) {
                                method.invoke(o);
                            }

                        }
                    }

                }


            }
            long l1 = System.currentTimeMillis();
            System.out.println(l1-l);
        }
    }

